/*
XPTracker - Alternative Agile Tracking Tool
Copyright (C) 2006-2009  Stephen Starkey

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
*/
package org.xptracker.web.tags;

import org.apache.commons.lang.StringUtils;
import org.xptracker.domain.State;
import org.xptracker.domain.Story;
import org.xptracker.services.StoryState;

import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.jstl.core.LoopTagSupport;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

public class ListActionStatesTag extends LoopTagSupport {
  private Iterator<State> statesI;
  private Story story;
  private String stateVar;
  private String descriptionVar;

  public Story getStory() {
    return story;
  }

  public void setStory(Story story) {
    this.story = story;
  }

  public String getStateVar() {
    return stateVar;
  }

  public void setStateVar(String stateVar) {
    this.stateVar = stateVar;
  }

  public String getDescriptionVar() {
    return descriptionVar;
  }

  public void setDescriptionVar(String descriptionVar) {
    this.descriptionVar = descriptionVar;
  }

  //todo Move all this logic into StoryState.getNextUserActionStates or something..
  protected void prepare() throws JspTagException {
    State[] nextStates = StoryState.getNextActionStates(story);
    List<State> states = new ArrayList<State>(Arrays.asList(nextStates));
    statesI = states.iterator();
    while (statesI.hasNext()) {
      State s = statesI.next();
      if (!StoryState.getInfo(s).managedByUser() || (s == State.dreaming && story.getQueue() != null)) {
        statesI.remove();
      }
    }

    if (story.getState() == State.inProcess && story.getQueue() != null) {
      states.add(State.inQueue);
    }
    statesI = states.iterator();
  }

  protected boolean hasNext() throws JspTagException {
    return statesI.hasNext();
  }

  protected Object next() throws JspTagException {
    State nextState = statesI.next();
    State currentState = story.getState();

    String verb = StoryState.getRealizationVerb(currentState, nextState);
    String stateVar = StringUtils.isBlank(getStateVar()) ? "state" : getStateVar();
    String descVar = StringUtils.isBlank(getDescriptionVar()) ? "description" : getDescriptionVar();

    pageContext.setAttribute(stateVar, nextState);
    pageContext.setAttribute(descVar, verb);
    return nextState;
  }
}
